//
//  userinfoHeaderTableViewCell.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/2/9.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "userinfoHeaderTableViewCell.h"
#import "SDCycleScrollView.h"
#import "CYUserinfoHeaderBottom.h"

@implementation userinfoHeaderTableViewCell
{
    SDCycleScrollView *cycleScrollView;
    UILabel *_nickname;
    QMUIButton *_level;
    UIImageView *_sex;
    UIImageView *auth;//认证
    QMUIButton *online;//在线状态
    QMUIButton *buttonCall;//通话时长
    QMUIButton *local;  //位置
    
    UILabel *attrlab;//关注
    UILabel *fans;//粉丝
    UILabel *give_like;//获赞数
    
    QMUIFillButton *attrButton;//关注按钮
    UILabel *video_deduction;//视频扣费
    CYUserinfoHeaderBottom *bottom;
    UILabel *ids;
    UIView *line1;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    //添加一个轮播图
    
    NSArray *imagesURLStrings = @[
                                  ];
    
    cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenWidth) delegate:self placeholderImage:nil];
    cycleScrollView.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"];
    cycleScrollView.pageDotImage = [UIImage imageNamed:@"pageControlDot"];
    cycleScrollView.imageURLStringsGroup = imagesURLStrings;
    cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:cycleScrollView];
    
    //昵称
    _nickname = [[UILabel alloc] init];
    _nickname.text = ASLocalizedString(@"昵称");
    _nickname.font = DEFAULT_FONT(15);
    _nickname.textColor = KWhiteColor;
    [self addSubview:_nickname];
    
    ids = [[UILabel alloc] init];
    ids.text = @"ID:";
    ids.font = DEFAULT_FONT(15);
    ids.textColor = KWhiteColor;
    [self addSubview:ids];

    
    //添加等级
    _level = [[QMUIButton alloc] init];
    [_level setTitle:@"M3" forState:UIControlStateNormal];
    [_level setBackgroundColor:MainColor2];
    [_level setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_level setContentEdgeInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
    _level.titleLabel.font = DEFAULT_FONT(11);
    ViewRadius(_level, 4);
    
    [self addSubview:_level];
    
    _sex = [[UIImageView alloc] init];
    _sex.image = IMAGE_NAMED(@"female");
    [self addSubview:_sex];
    
    
    //认证状态
    auth = [[UIImageView alloc] init];
    auth.image = IMAGE_NAMED(@"verified_icon");
    [self addSubview:auth];
    
    online = [[QMUIButton alloc] init];
    UIImage *dotimage;
    online.titleLabel.font = DEFAULT_FONT(12);
    [online setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if([_model.is_online isEqualToString:@"1"]){
        UIImage *radImage = [[UIImage imageWithColor:MainColor size:CGSizeMake(3, 3)] imageByRoundCornerRadius:1.5];
        dotimage = radImage;
        [online setTitle:ASLocalizedString(@"在线")forState:UIControlStateNormal];
    }
    else
    {
        UIImage *radImage = [[UIImage imageWithColor:CLightGray size:CGSizeMake(3, 3)] imageByRoundCornerRadius:1.5];
        dotimage = radImage;
        [online setTitle:ASLocalizedString(@"离线")forState:UIControlStateNormal];
    }
    
    [online setImage:dotimage forState:UIControlStateNormal];
    ViewRadius(online, 3);
    online.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    [self addSubview:online];
    
    buttonCall = [[QMUIButton alloc] init];

//    [buttonCall setImage:IMAGE_NAMED(@"face_time_icon") forState:UIControlStateNormal];
    [buttonCall setTitle:ASLocalizedString(@"通话22h")forState:UIControlStateNormal];
    buttonCall.titleLabel.font = DEFAULT_FONT(12);
    [buttonCall setTitleColor:KWhiteColor forState:UIControlStateNormal];
//    buttonCall.imageView.contentMode = UIViewContentModeScaleAspectFit;
    buttonCall.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    [self addSubview:buttonCall];
    
    
    
    local = [[QMUIButton alloc] init];
    [local setImage:IMAGE_NAMED(@"location") forState:UIControlStateNormal];
    [local setTitle:ASLocalizedString(@"北京")forState:UIControlStateNormal];
    local.titleLabel.font = DEFAULT_FONT(12);
    [local setTitleColor:KWhiteColor forState:UIControlStateNormal];
    local.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    [self addSubview:local];
    
    video_deduction = [[UILabel alloc] init];
    video_deduction.text = NSStringFormat(ASLocalizedString(@"每分钟%@%@"),@"..",KCOIN_NAME);
    video_deduction.font = DEFAULT_FONT(12);
    video_deduction.textColor = KWhiteColor;
    [self addSubview:video_deduction];
    
    attrButton = [[QMUIFillButton alloc] initWithFillColor:MainColor titleTextColor:[UIColor whiteColor]];
    [attrButton addTarget:self action:@selector(doAttr) forControlEvents:UIControlEventTouchUpInside];
    attrButton.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [attrButton setTitle:ASLocalizedString(@"+ 关注")forState:UIControlStateNormal];
    [self addSubview:attrButton];
    
    _ContributionList = [[UIButton alloc] init];
    [_ContributionList setTitle:ASLocalizedString(@"贡献榜")forState:UIControlStateNormal];
    [_ContributionList setBackgroundImage:[UIImage imageNamed:@"user_contributeRank"] forState:UIControlStateNormal];
    [_ContributionList setTitleColor:[UIColor colorWithHexString:@"d8c66a"] forState:UIControlStateNormal
     ];
    _ContributionList.titleLabel.font = [UIFont systemFontOfSize:12];
    _ContributionList.contentHorizontalAlignment = NSTextAlignmentRight;
    
    
    [self addSubview:_ContributionList];
    [_ContributionList addTarget:self action:@selector(handleContributionEvent) forControlEvents:UIControlEventTouchUpInside];
    
    line1 = [[UIView alloc] init];
    line1.backgroundColor = RGBOF(0xF4F4F4);
    [self addSubview:line1];
    
    //底部view
    bottom = [CYUserinfoHeaderBottom getView];
    bottom.backgroundColor = [UIColor clearColor];
    [bottom.btnattr addTarget:self action:@selector(doAttr) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:bottom];
    
    [self setLayout];
    
}

//跳转贡献榜
- (void)handleContributionEvent {
    
}

-(void)doAttr
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"personal_api" A:@"click_attention"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:_model.id forKey:@"id"];
    
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        if([_model.attention isEqualToString:@"1"])
        {
            _model.attention = @"2";
            [attrButton setFillColor:MainColor];
            [attrButton setTitle:ASLocalizedString(@"+ 关注")forState:UIControlStateNormal];
            
            [bottom.btnattr setTitle:ASLocalizedString(@"+ 关注")forState:UIControlStateNormal];
            [bottom.btnattr setFillColor:MainColor];
        }
        else
        {
            _model.attention = @"1";
            //            [bottom.btnattr setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
            [attrButton setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
            
            [attrButton setFillColor:CLightGray];
            [bottom.btnattr setFillColor:CLightGray];
        }
        
        //        [MBProgressHUD showTopTipMessage:ASLocalizedString(@"关注成功")isWindow:YES];
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}

-(void)setLayout
{
    [_nickname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self.mas_bottom).offset(-126);
    }];
    
    [ids mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(_nickname.mas_bottom).offset(2);
    }];

    [_level mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nickname.mas_right).offset(5);
        make.centerY.equalTo(_nickname);
//        make.top.equalTo(_nickname.mas_bottom).offset(10);
        make.width.equalTo(@30);
        make.height.equalTo(@17);
    }];
    
    [_sex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_level.mas_height);
        make.height.equalTo(_level.mas_height);
        make.centerY.equalTo(_level);
        make.left.equalTo(_level.mas_right).offset(5);
    }];
    
    [auth mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_sex);
        make.width.equalTo(_level.mas_height);
        make.height.equalTo(_level.mas_height);
        make.centerY.equalTo(_level);
        make.left.equalTo(_sex.mas_right).offset(5);
    }];
    
    [online mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@30);
        make.height.equalTo(_level.mas_height);
        make.left.equalTo(auth.mas_right).offset(5);
        make.centerY.equalTo(_sex);
    }];
    
    [buttonCall mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ids.mas_bottom).offset(10);
        make.left.equalTo(_nickname);
        make.height.equalTo(@14);
    }];
    
    [local mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(buttonCall);
        make.left.equalTo(buttonCall.mas_right).offset(15);
        make.width.equalTo(buttonCall);
        make.height.equalTo(buttonCall);
    }];
    
    [video_deduction mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.centerY.equalTo(buttonCall);
        make.left.equalTo(buttonCall);
        make.width.equalTo(buttonCall);
        make.top.equalTo(buttonCall.mas_bottom).offset(2);
        make.height.equalTo(buttonCall);
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.width.equalTo(self);
        make.top.equalTo(video_deduction.mas_bottom).offset(3);
    }];
    
//    bottom.backgroundColor = [UIColor redColor];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self).offset(0);
        make.top.equalTo(line1.mas_bottom).offset(0);
        make.width.equalTo(self);
        make.height.equalTo(@50);
    }];
    
    [attrButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(0);
        make.top.equalTo(_nickname).offset(20);
        make.width.equalTo(@60);
        make.height.equalTo(@25);
    }];
    
    [_ContributionList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(0);
        make.top.equalTo(self).offset(kScreenHeight * 0.2);
        make.width.equalTo(@65);
        make.height.equalTo(@30);
    }];

}

-(void)setModel:(UserPageInfoModel *)model
{
    _model = model;
    NSMutableArray *picturesArr = [NSMutableArray array];
    for (PicturesFULL *model in _model.img) {
        [picturesArr addObject:[cyurl safeUrl:model.img]];
    }
    
    ids.text = [NSString stringWithFormat:@"ID:%@",model.id];
    if([_model.id isEqualToString:curUser.id])
    {
        attrButton.hidden = YES;
    }
    if(picturesArr.count == 0)
    {
        [picturesArr addObject:safeurl(model.avatar)];
    }
    
    cycleScrollView.imageURLStringsGroup = picturesArr;
    _nickname.text = model.user_nickname;
    
    video_deduction.text = [NSString stringWithFormat:ASLocalizedString(@"每分钟%@%@"),model.video_deduction,KCOIN_NAME];
    
    //等级
    if(model.sex == 1)//男
    {
        [_level setTitle:[NSString stringWithFormat:@"V%ld",(long)_model.level] forState:UIControlStateNormal];
        _sex.image = IMAGE_NAMED(@"male");
    }
    else//女
    {
        [_level setTitle:[NSString stringWithFormat:@"M%ld",(long)_model.level] forState:UIControlStateNormal];
        _sex.image = IMAGE_NAMED(@"female");
    }
    
    //"user_status":状态 0禁用 1正常 2未验证,
    
    if(model.user_status == 1)
    {
        //认证成功通过
    }
    else
    {
        auth.hidden = YES;
        [auth mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_sex);
            make.width.equalTo(@1);
            make.height.equalTo(_level.mas_height);
            make.centerY.equalTo(_level);
            make.left.equalTo(_sex.mas_right).offset(5);
        }];
    }
    
    [buttonCall setTitle:[NSString stringWithFormat:ASLocalizedString(@"通话:%@"),_model.call] forState:UIControlStateNormal];
    [local setTitle:_model.address forState:UIControlStateNormal];
    
    bottom.fansnumber.text = [NSString stringWithFormat:ASLocalizedString(@"粉丝：%ld"),(long)model.attention_fans];
    bottom.attrLable.text = [NSString stringWithFormat:ASLocalizedString(@"关注：%ld"),(long)model.attention_all];
    bottom.getlike.text = [NSString stringWithFormat:ASLocalizedString(@"获赞数：%ld"),(long)model.give_like];
    
    //关注按钮         "attention": "用户是否关注 1关注2不关注",
    if([model.attention isEqualToString:@"1"])
    {
        [attrButton setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
        [attrButton setFillColor:CLightGray];
        
        [bottom.btnattr setTitle:ASLocalizedString(@"已关注")forState:UIControlStateNormal];
        [bottom.btnattr setFillColor:CLightGray];
    }
    
    UIImage *dotimage;
    [online setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if([_model.is_online isEqualToString:@"1"]){
        UIImage *radImage = [[UIImage imageWithColor:MainColor size:CGSizeMake(3, 3)] imageByRoundCornerRadius:1.5];
        dotimage = radImage;
        [online setTitle:ASLocalizedString(@"在线")forState:UIControlStateNormal];
    }
    else
    {
        UIImage *radImage = [[UIImage imageWithColor:CLightGray size:CGSizeMake(3, 3)] imageByRoundCornerRadius:1.5];
        dotimage = radImage;
        [online setTitle:ASLocalizedString(@"离线")forState:UIControlStateNormal];
    }
    [online setImage:dotimage forState:UIControlStateNormal];

    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

